<?php

class TwitterSearch
{
	protected $encoded_query;
	protected $search_url;
	protected $max_tweets;
	protected $hide_replies;
	protected $tweets;

	public function __construct( $query, $max_tweets, $hide_replies )
	{
		$this->load_magpie();
		$this->construct_search_url( $query );
		$this->max_tweets = $max_tweets;
		$this->hide_replies = $hide_replies;
	}

	public function tweets()
	{
		$this->do_search();
		return (array) $this->tweets;
	}
	
	protected function construct_search_url( $query )
	{
	

		// Effing slashes
		$query = stripslashes( $query );
		$query = str_replace(" ", "+", $query);
		$args = array();
		
		if (!is_front_page()) 
			$query = $this->search_url_from_categories();
		
		$query = stripslashes( $query );

		var_dump($query);print_r('<br>');

		$args[ 'q' ] = $query;
		$args[ 'rpp' ] = 50;
		$args[ 'lang' ] = 'es';
		$base_url = 'http://search.twitter.com/search.atom';
		$this->search_url = add_query_arg( $args, $base_url );
		var_dump($this->search_url);
	}
	
	protected function search_url_from_categories() {

		$infixString = '+OR+';
		
		$categories = get_the_category();
		$query = '';

		for($i = 0; $i < sizeof($categories); $i += 1) {
			$category = $categories[$i];

		 	if ($i +1== sizeof($categories)) {
				$query .= $category->cat_name;
		 	} else {
			 	$query .= $category->cat_name . $infixString;
		 	
		 	}
		}
		//$query = str_replace(" ", "+", $query);

		return $query;
		
		
	}
	
	
	protected function do_search()
	{
		// N.B. Cache is implemented by Magpie. Phew.
		$rss = @ fetch_rss( $this->search_url );
		
		if ( is_array( $rss->items ) && ! empty( $rss->items ) ) {
			// Retrieve number of tweets from the options
			$max_tweets = (int) @ $this->max_tweets;
			if ( ! $max_tweets ) $max_tweets = 3;

			// loop through each item in the feed
			foreach( $rss->items as & $item ){
				if ( $this->hide_replies && $this->is_reply( $item[ 'atom_content' ] ) ) {
					continue;
				}
				$this->tweets[] = new Tweet(
						$item[ 'atom_content' ],
						$item[ 'link' ],
						$item[ 'published' ],
						$item[ 'author_name' ],
						$item[ 'author_uri' ],
						$item[ 'link_image' ]
					);
				// Stop once we've got enough tweets
				if ( count( $this->tweets ) == $max_tweets ) break;
			}
		}
	}
	
	protected function load_magpie()
	{
		if (function_exists('fetch_rss')) return true;

		$wp_includes = ABSPATH . WPINC;
		if ( file_exists( $wp_includes . '/rss.php' ) ) {
			require_once( $wp_includes . '/rss.php');
		} elseif ( file_exists( $wp_includes . '/rss-functions.php' ) ) {
			require_once(  $wp_includes . '/rss-functions.php' );
		}

		if ( function_exists( 'fetch_rss' ) ) return true;
		return false;
	}

	protected function is_reply( $content )
	{
		// Strip tags (we're being passed HTML)
		$content = strip_tags( $content );
		// Ensure no leading whitespace
		$content = trim( $content );
		// Is the first character an '@' sign?
		return ( substr( $content, 0, 1 ) == '@' );
	}

}

?>
